from selenium import webdriver
from time import sleep # 强制等待
from selenium.webdriver.common.keys import Keys   # 键盘操作
from selenium.webdriver.common.by import By       # 定位方式
from selenium.webdriver.common.action_chains import ActionChains  # 鼠标操作
from selenium.webdriver.support.select import Select   # 下拉框
from selenium.webdriver.support import expected_conditions as EC   # 判断元素是否存在/可见
from selenium.webdriver.support.wait import WebDriverWait        #显示等待

class Broswer():
    def __init__(self,type='gc'):
        if type == 'gc':
            self.driver = webdriver.Chrome()
        elif type =='ie':
            self.driver = webdriver.Ie()
        elif type =='ff':
            self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
    def open_url(self,url):
        """
        打开被测网站
        :param url:网站url地址
        :return:
        """
        self.driver.get(url)

    def get_element(self,location,timeout=20,poll=0.5):
        """
        根据传入的元素定位方式,定位元素并返回
        :param location:   元素定位方式(元组类型)
        :param timeout:
        :param poll:
        :return: 返回定位到的元素标签
        """
        try:
            WebDriverWait(self.driver,timeout,poll_frequency=poll).until(EC.visibility_of_element_located(location))
            ele =self.driver.find_element(*location)
            return ele
        except Exception as e:
            print('定位方式为:%s,元素定位失败!!!'%location)
    def input_text(self,location,value):
        """
        输入操作
        :param location:元素定位方式
        :param value: 输入内容
        :return:
        """
        ele = self.get_element(location)
        try:
            ele.send_keys(value)
        except Exception as e:
            print("输入操作失败!!!")
    def click_element(self,location):
        """
        点击操作
        :param location: 元素定位方式
        :return:
        """
        ele = self.get_element(location)
        try:
            ele.click()
        except Exception as e:
            print("点击操作失败!!!")
    def switch_to_window(self,idx=-1):
        """
        切换窗口
        :param idx: 窗口所在的下标(默认为-1,取最后一个)
        :return:
        """
        self.driver.switch_to.window(self.driver.window_handles(idx))

    def switch_into_frame(self,location):
        """
        切入frame内
        :param location:  frame 定位方式
        :return:
        """
        frame = self.get_element(location)
        self.driver.switch_to.frame(frame)
    def switch_outo_frame(self):
        """
        切出frame
        :return:
        """
        self.driver.switch_to.default_content()
    def switch_alert(self,action='d',value=None):
        """
        切换alert弹框
        :param action:切入的操作  a:确定  d:取消    outo:输入并确定
        :param value:  value不为None时,代表输入的内容
        :return:
        """
        alert = self.driver.switch_to.alert
        if action == 'a':
            alert.accept()
        elif action == 'd':
            alert.dismiss()
        else:
            alert.send_keys(value)
            alert.accept()

    def execute_script(self,script,ele=None):
        """
        执行指定的javascript脚本语句
        :param script:  javascript  脚本语句(str)
        :param ele:
        :return:
        """
        self.driver.execute_script(script,ele)
    def get_element_text(self,location):

        '''
        获取元素标签的文本值
        :param location:
        :return:
        '''
        ele = self.get_element(location)
        return ele.text
    def get_element_attribute(self,location,attribute):
        """
        获取标签属性值
        :param location:元素定位方式
        :param attribute: 属性名称
        :return: 获取到的属性值
        """
        ele = self.get_element(location)
        try:
            attr = ele.get_attribute(attribute)
            return attr
        except Exception as e:
            print("获取元素属性值失败%s"%attribute)
    def execute_selenium_script(self,location,method,attribute):
        """
        selenium 定位元素,使用javascript执行
        :param location: 元素定位方式
        :param method:   javascript  操作方法
        :param attribute:   属性名称
        :return:
        """
        ele =self.get_element(location)
        js = 'arguments[0].%s("%s")'%(method,attribute)
        print(js)
        self.execute_script(js,ele)
    def selected_action(self,location,idx=None,value=None,text=None):
        """
        下拉框操作
        :param location:元素定位方式
        :param idx: 下标
        :param value: value属性
        :param text: 文本值
        :return:
        """
        ele = self.get_element(location)
        select = Select(ele)
        if idx is not None:
            select.select_by_index(idx)
        if value is not None:
            select.select_by_value(text)
        if text is not None:
            select.select_by_visible_text(idx)
    def back_broswer(self):
        self.driver.back()
    def forward_broswer(self):
        self.driver.forward()
    def scrollTo_window(self,x,y):     #窗口滚动条拖拽    x坐标轴 y坐标轴
        js = 'window.scrollTo(%d,%d)'%(x,y)
        self.execute_script(js)

    def clear_element(self,location):
        ele = self.get_element(location)
        ele.clear()
    def quit_driver(self):
        self.driver.quit()
    def get_photo_as_file(self,filename):    #截图操作
        self.driver.get_screenshot_as_file(filename)


if __name__ == '__main__':
    br = Broswer()
    br.open_url('http://www.baidu.com')
    input_loc = (By.ID, 'kw')
    br.execute_selenium_script(input_loc, 'removeAttribute', 'maxlength')









